package com.example.springbootcourse.service.impl;

import com.example.springbootcourse.mapper.PowerMapper;
import com.example.springbootcourse.mapper.UserMapper;
import com.example.springbootcourse.pojo.Power;
import com.example.springbootcourse.pojo.User;
import com.example.springbootcourse.pojo.UserExample;
import com.example.springbootcourse.req.UserQueryReq;
import com.example.springbootcourse.resp.UserResp;
import com.example.springbootcourse.service.UserService;
import com.example.springbootcourse.util.BusinessException;
import com.example.springbootcourse.util.BusinessExceptionCode;
import com.example.springbootcourse.util.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    PowerMapper powerMapper;

    @Override
    public PageInfo<User> findUser(UserQueryReq req) {

        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (!ObjectUtils.isEmpty(req.getName())){
            criteria.andNameLike("%"+req.getName()+"%");
        }
        criteria.andTypeEqualTo(req.getType());
        // 分页
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<User> users = userMapper.selectByExample(example);
        PageInfo<User> info = new PageInfo<>(users);
        return info;
    }

    @Override
    public List<User> queryUser(String type) {

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andTypeEqualTo(type);
        List<User> users = userMapper.selectByExample(userExample);

        return users;
    }

    @Override
    public void saveUser(User user) {

        User user1 = userMapper.selectByPrimaryKey(user.getId());

        if (ObjectUtils.isEmpty(user1)){
            userMapper.insert(user);
        }else {
            userMapper.updateByPrimaryKey(user);
        }

    }

    @Override
    public void delUser(String id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UserResp login(String userId, String password) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(userId);
        List<User> users = userMapper.selectByExample(userExample);

        if(CollectionUtils.isEmpty(users)){
            // 登录名不存在
            throw new BusinessException(BusinessExceptionCode.USER_LOGIN_ERROR);
        }else {
            // 登录名存在
            // 判断密码是否正确
            User userDB = users.get(0);
            if (userDB.getPassword().equals(password)){
                UserResp userResp = CopyUtil.copy(userDB, UserResp.class);

                Power power = powerMapper.selectByPrimaryKey(userResp.getType());
                userResp.setPower(power.getPower());

                return userResp;
            }else {
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_ERROR);
            }
        }

    }

    @Override
    public void upPwd(User user) {
        userMapper.updateByPrimaryKey(user);
    }
}
